<template>
  <div class="master-list">
      <s-master-list v-if="showSkeleton"></s-master-list>
      <ul v-else v-infinite-scroll="requestData" infinite-scroll-disabled="cellLoading" infinite-scroll-distance="20" infinite-scroll-immediate-check="false">
          <li v-for="(item,index) in masterData" :class="[{'notLast':index!=masterData.length-1}]" :key="item.id" @click="lookDetails(item)">
              <div class="left">
                <my-image class="my-img fade" :imgUrl="item.imgUrl"></my-image>
              </div>
              <div class="content">
                  <div class="top">
                    <span class="name">{{item.name}}</span>
                    <span class="job">{{item.job}}</span>
                  </div>
                  <div class="middle">
                    <p class="dec-item" v-for="(dec,decIndex) in item.decs" :key="decIndex">
                      {{dec}}
                    </p>
                  </div>
                  <div class="bottom">
                    <div class="appointment-num">预约数: {{item.bespokenum}}</div>
                    <div class="appointment-btn">
                      <span class="appointment-word">预约</span>
                    </div>
                  </div>
              </div>
          </li>
      </ul>
  </div>
</template>

<script>
import { mapState } from "vuex";
import sMasterList from "../skeletons/sMasterList.vue";
import vCard from "./activity/card.vue";
import myImage from "../lazyloadImg/lazyImage.vue";
import Vue from "vue";
import { InfiniteScroll } from "mint-ui";
import { savaToLocal, loadFromlLocal } from "../../common/js/storage.js"; // 公共方法：本地缓存
Vue.use(InfiniteScroll);

export default {
  name: "master-list",
  data() {
    return {
      masterData: [],
      jobConfig: null,
      cellLoading: true,
      curPage: 0,
      pageNum: 10,
      existIdArr: [],
      showSkeleton: true
    };
  },
  components: { vCard, myImage, sMasterList },
  activated() {},
  mounted() {
    this.requestData(false);
  },
  methods: {
    requestData(infinite = true) {
      this.$store.dispatch("toggleLoading", true);
      this.$http
        .post(this.path + "store/Beautician/Beauticianlist", {
          page: this.curPage,
          storeid: this.storeid
        })
        .then(response => {
          this.$store.dispatch("toggleLoading", false);
          if (this.showSkeleton) this.showSkeleton = false;
          if (infinite && this.curPage == 0) return;
          if (infinite && this.cellLoading) return;
          this.cellLoading = true;
          if (!this.jobConfig) this.initJobConfig(response.body.result.role);
          let serverData = response.body.result.barr;
          if (!serverData || serverData.length == 0) {
            //数据为空
            this.cellLoading = true;
            if (this.curPage == 0) this.showEmpty = true;
          } else {
            //有数据
            if (serverData.length % 10 == 0) {
              //满一页数据
              this.curPage++;
              this.cellLoading = false;
            } else {
              //不满一页
              this.cellLoading = true;
            }
            let detailsArr = this.analysisData(serverData);
            for (let i = 0; i < detailsArr.length; i++) {
              let orderid = detailsArr[i].id;
              if (this.existIdArr.indexOf(orderid) >= 0) continue;
              this.masterData.push(detailsArr[i]);
              this.existIdArr.push(orderid);
            }
          }
        })
        .catch(response => {
          console.log(response);
          this.$store.dispatch("toggleLoading", false);
        });
    },
    analysisData(data) {
      let detailsArr = [];
      data.forEach((val, index, arr) => {
        detailsArr.push({
          id: val.beautician_id,
          imgUrl: this.soursePath + val.icon,
          name: val.name,
          job: this.jobConfig[val.role],
          decs: val.message.split("\n"),
          bespokenum: val.bespokenum,
          exp:val.exp,
          phone:val.phone,
          level:val.level,
          goodAt:val.workmsg,
          curAppointment:1,
          leftAppointment:8
        });
      });
      return detailsArr;
    },
    initJobConfig(originalData) {
      this.jobConfig = {};
      originalData.forEach((val, index, arr) => {
        this.jobConfig[val.id] = val.rolename;
      });
    },
    lookDetails(item) {
      savaToLocal('dianwu2',"masterInfo",item);
      this.$router.push({path: "/masterDetails"});
    }
  },
  computed: {
    ...mapState(["openid", "path", "userid", "storeid", "soursePath"])
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.master-list {
  box-sizing: border-box;
  width: 100%;
  min-height: 100%;
  background-color: #f2f2f2;
  padding-bottom: 0.09rem;

  ul {
    width: 100%;

    li {
      width: 100%;
      padding: 0.09rem 0.09rem;
      background-color: #ffffff;
      box-sizing: border-box;
      display: flex;
      align-items: center;

      &.notLast {
        margin-bottom: 0.09rem;
      }

      .left {
        align-self: flex-start;
        width: 0.75rem;
        height: 0.75rem;

        .img-container {
          border-radius: 0.02rem;
          height: 100%;
          width: 100%;
          background-color: #f2f2f2;

          .my-img {
            background-color: #f2f2f2;
          }
        }
      }

      .content {
        flex: 1;
        margin-left: 0.09rem;

        .top {
          margin-top: 0.03rem;

          .name {
            color: #333;
            font-size: 0.16rem;
            font-weight: 600;
            letter-spacing: 0.01rem;
          }

          .job {
            margin-left: 0.02rem;
            font-size: 0.13rem;
            color: #aaa;
          }
        }

        .middle {
          margin-top: 0.07rem;

          .dec-item {
            color: #aaa;
            font-size: 0.13rem;
            line-height: 0.18rem;
          }
        }

        .bottom {
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-top: 0.07rem;
          margin-bottom: 0.04rem;

          .appointment-num {
            color: #ff84bb;
            font-size: 0.14rem;
          }

          .appointment-btn {
            background-color: #e6faef;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 0.54rem;
            height: 0.24rem;
            border-radius: 0.04rem;

            .appointment-word {
              color: #2bcb81;
              font-size: 0.12rem;
              letter-spacing: 0.02rem;
              font-weight: 600;
              margin-top: 0.01rem;
            }
          }
        }
      }
    }
  }
}
</style>
